package com.nfu.zlsp.dev.controller;

import com.nfu.zlsp.dev.common.constants.TheConstant;
import com.nfu.zlsp.dev.common.utils.PageUtils;
import com.nfu.zlsp.dev.common.utils.R;
import com.nfu.zlsp.dev.entity.AdminEntity;
import com.nfu.zlsp.dev.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Map;



/**
 *
 *
 * @author txh
 * @email hunterapeman@gmail.com
 * @date 2023-03-12 01:54:16
 */
@RestController
@RequestMapping("dev/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
    public R login(@RequestBody AdminEntity admin, HttpServletResponse response, HttpSession session){
        AdminEntity adminEntity = adminService.queryAdminForLogin(admin);
        if(adminEntity != null){
            session.setAttribute(TheConstant.USER_INFO,adminEntity);

            if(admin.getIsRemMe() == true){

                //记住用户账户  把信息放入cookie
                Cookie c1 = new Cookie("loginActCookie",admin.getNumber());
                c1.setMaxAge(10 * 24 * 60 * 60);
                c1.setPath("/");
                response.addCookie(c1);
                Cookie c2 = new Cookie("loginPwdCookie", admin.getAdminpassword());
                c2.setMaxAge(10 * 24 * 60 * 60);
                c2.setPath("/");
                response.addCookie(c2);
            }else{

                //删除cookie
                Cookie c1 = new Cookie("loginActCookie","1");
                c1.setMaxAge(0);
                c1.setPath("/");
                response.addCookie(c1);
                Cookie c2 = new Cookie("loginPwdCookie", "1");
                c2.setMaxAge(0);
                c2.setPath("/");
                response.addCookie(c2);
            }

            return R.ok();
        }else {
            return R.ok().put("data","用户名或密码错误");
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("dev:admin:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = adminService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("dev:admin:info")
    public R info(@PathVariable("id") String id){
		AdminEntity admin = adminService.getById(id);

        return R.ok().put("admin", admin);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("dev:admin:save")
    public R save(@RequestBody AdminEntity admin){
		adminService.save(admin);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("dev:admin:update")
    public R update(@RequestBody AdminEntity admin){
		adminService.updateById(admin);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("dev:admin:delete")
    public R delete(@RequestBody String[] ids){
		adminService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
